package com.hl.card.pay.service.impl;

import com.hl.card.modular.system.service.ISysDicService;
import com.hl.card.pay.dto.Checker;
import com.hl.card.pay.service.CheckHandlerService;
import com.hl.card.pay.vo.ChangeVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CheckHandlerServiceImpl implements CheckHandlerService {
    @Value("${pay.redis.exchange.daylimitPrefix}")
    private String dayPrefix;
    @Value("${pay.redis.exchange.monthlimitPrefix}")
    private String monthPrefix;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private ISysDicService sysDicService;
    /**
     * 单笔限额100 每日限额 500 每月限额 1000
     */
    @Override
    public Checker exChangeCheckHandler(ChangeVo vo) {
        Checker checker=new Checker();
        Map<String, Object> item2 =  sysDicService.selectListByCodeNo("BDDHMZ", vo.getCode());
        String amount= (String) item2.get("dicValue");
        int intAmount=Integer.parseInt(amount);
        int onceLimit=100,dayLimit=500,monthLimit=1000;
        String userId=vo.getUserId();
        Integer  dayInt=getIntValue(dayPrefix+userId);
        Integer  monthInt=getIntValue(monthPrefix+userId);
        if(intAmount>onceLimit){//超出单笔限额
            checker.setPass(false);
            checker.add("errorCode","1");
            checker.add("error","超出单笔限额");
        }else if(intAmount>dayLimit-dayInt){//超出每日限额
            checker.setPass(false);
            checker.add("errorCode","2");
            checker.add("error","超出每日限额");
        }else if(intAmount>monthLimit-monthInt){//超出月限额
            checker.setPass(false);
            checker.add("errorCode","3");
            checker.add("error","超出月限额");
        }
        return checker;
    }
    public String getValue(String key){
        return redisTemplate.opsForValue().get(key);
    }
    public Integer getIntValue(String key){
        String value=getValue(key);
        if(StringUtils.isNumeric(value)){
            return Integer.parseInt(value);
        }else{
            return 0;
        }
    }


}
